package com.sdtele.tta.operation;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.User;
import com.sdtele.tta.models.VoteTask;


/**
 * 操作类实现：GetUser(json.operation=getUser)
 * 
 * @author XCream
 */
public class OperationChgVoteTask extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		User curUser = (User) request.getSession().getAttribute("user");
		long voteId = jsonRequest.getJsonObject().get("voteId").getAsLong();
		
		VoteTask votetask = Dac.getInstance().getVoteTaskbyVoteid(voteId);

		if (!curUser.equals(null)) {
			if (curUser.getId() ==  votetask.getOwnerId()) {
				String title = jsonRequest.getJsonObject().get("title").getAsString();
				String description = jsonRequest.getJsonObject().get("description").getAsString();
				String deadLineStr = jsonRequest.getJsonObject().get("deadLine").getAsString();
				System.out.println(deadLineStr);
				DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				if(deadLineStr !=null)
				{
				java.util.Date ud = new java.util.Date();
				try {
					ud = sdf.parse(deadLineStr);
				} catch (ParseException e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}
				Date deadLine = new java.sql.Date(ud.getTime());
				System.out.println(deadLine);
				
				 votetask.setDeadLine(deadLine);
				}
				Integer  voteType = jsonRequest.getJsonObject().get("voteType").getAsInt();
				if (title.isEmpty()) {
					jres.setStatus(1);
					jres.setMessage("投票任务标题不允许为空");
					return jres;
				}
				if(!(title.equals(null))) {
					votetask.setTitle(title);
				}
				if(!(description.equals(null))) {
					 votetask.setDescription(description);
				}

				if(!(voteType.equals(null))) {
					votetask.setVoteType(voteType);
				}
				 
		
				 if (Dac.getInstance().chgVoteTask( votetask)) {
					jres.setStatus(0);
					jres.setMessage("修改投票任务成功");
					jres.setEntity(votetask);
				} else {
					jres.setStatus(1);
					jres.setMessage("修改投票任务失败");
				}
			} else {
				jres.setStatus(1);
				jres.setMessage("您没有权限修改此投票 任务");
			}
		} else {
			jres.setStatus(1);
			jres.setMessage("用户未登录");
		}
		return jres;
	}

}